<?php 
session_start();
include 'includes/functions.php';
db();

$id = $_GET['select'];
$power = $_GET['power'];
$me = $_SESSION['user_info']['user'];
$time = time();

$time_q = mysql_query('SELECT `fight` FROM users WHERE user="'.$me.'"');
$time_f = mysql_fetch_assoc($time_q);
$math = $time-$time_f['fight'];

    

    if($math>=600){
        
	if($id==1){
		$rs = mysql_query('SELECT user FROM users WHERE power<61 AND user!="'.$me.'"');
                $count = mysql_num_rows($rs);
                $rand = rand(1,$count);
                $i = 0;
		while($row = mysql_fetch_assoc($rs)){
                    $i++;
                        if($rand==$i){
                            $name = $row['user'];
                        }
                }
                
                mysql_query('UPDATE users SET money=money+100,rep=rep+1 WHERE user="'.$me.'"');
                mysql_query('UPDATE users SET fight='.time().' WHERE user="'.$me.'"');
                if(!mysql_error()){
                    echo 'Поздравления! Ти преби '.$name.'. Ти получаваш $100 и +1 репутация';
                }
	}
	elseif($id==2){
            $rs = mysql_query('SELECT user FROM users WHERE user!="'.$me.'"');
                $count = mysql_num_rows($rs);
                $rand = rand(1,$count);
                $i = 0;
		while($row = mysql_fetch_assoc($rs)){
                    $i++;
                        if($rand==$i){
                            $name = $row['user'];
                        }
                }
                
                $power_q = mysql_query('SELECT power FROM users WHERE user="'.$name.'"');
                $power_f = mysql_fetch_assoc($power_q);
                
                if($power>$power_f['power']){
                    mysql_query('UPDATE users SET money=money+300,rep=rep+3 WHERE user="'.$me.'"');
                    mysql_query('UPDATE users SET fight='.time().' WHERE user="'.$me.'"');
                    if(!mysql_error()){
                        echo 'Поздравления! Ти преби '.$name.'. Ти получаваш $300 и 3 репутация';
                    }
                }
                else{
                    mysql_query('UPDATE users SET money=money-200,rep=rep-1 WHERE user="'.$me.'"');
                    mysql_query('UPDATE users SET fight='.time().' WHERE user="'.$me.'"');
                    if(!mysql_error()){
                        echo $name.' те преби. Ти загуби $200 и 1 репутация';
                    }
                }
	}
    }
    else{
        echo 'Можеш да атакуваш на всеки 10 мин.';
    }